/**
 * @module vim-loader
 */

import { MeshSection } from 'vim-format'
import { G3dSubset } from './g3dSubset'

export class G3dMeshCounts {
  instances: number = 0
  meshes: number = 0
  indices: number = 0
  vertices: number = 0
}

/**
 * Holds the offsets needed to preallocate geometry for a given meshIndexSubset
 */
export class G3dMeshOffsets {


  // computed
  readonly counts: G3dMeshCounts
  private _indexOffsets: Int32Array
  private _vertexOffsets: Int32Array

  /**
   * 给出指定子集下的指定模型类型的偏移量
   * @param subset subset for which to compute offsets
   * @param section 'opaque' | 'transparent' | 'all'
   */
  constructor(public readonly subset: G3dSubset, public readonly section: MeshSection) {

    this.counts = subset.getAttributeCounts(section)
    const meshCount = subset.meshCount
    this._indexOffsets = new Int32Array(meshCount)
    this._vertexOffsets = new Int32Array(meshCount)

    for (let index = 1; index < meshCount; index++) {
      this._indexOffsets[index] = this._indexOffsets[index - 1] + subset.getMeshIndexCount(index - 1, section)
      this._vertexOffsets[index] = this._vertexOffsets[index - 1] + subset.getMeshVertexCount(index - 1, section)

    }

  }


  /**
   *给出指定mesh索引的索引偏移量
   */
  getIndexOffset(meshIndex: number) {
    return meshIndex < this.counts.meshes
      ? this._indexOffsets[meshIndex]
      : this.counts.indices
  }

  /**
   * 给出指定mesh的顶点索引偏移量
   */
  getVertexOffset(mesh: number) {
    return mesh < this.counts.meshes
      ? this._vertexOffsets[mesh]
      : this.counts.vertices
  }

  /**
   * 给出指定mesh索引的实例长度
   */
  getMeshInstanceCount(meshIndex: number) {
    return this.subset.getMeshInstanceCount(meshIndex)
  }

  /**
   * 制定mesh索引的制定实例索引
   */
  getMeshInstance(mesh: number, index: number) {
    return this.subset.getMeshInstance(mesh, index)
  }

  /**
   * 给出内部mesh的原始索引
   */
  getSourceMesh(index: number) {
    return this.subset.getSourceMesh(index)
  }
}
